/**
 * Created by alice on 2017/5/04 0001
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        city: null,
        judge_uid: null,
        secretary: null,
        start: null,
        end: null,
        current_court_uid: null,
        case_type: null,
        _user_court_uid: null,
        page_size: 12,
        judge_secretary_list: null
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();
        function init_event() {//TODO 初始化页面事件
            var current_day = utils.get_url_param(0);
            //查询的开始时间默认给今天
            $('#startTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });
            var end_time = $('#endTime');
            $('#endTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });

            if (null != ready.start && "" != ready.start) {
                $("#startTime").find("input").val(ready.start);
            } else {
                var temp = new Date();
                var defaultStartTime = temp.getFullYear() + "-01-01";
                $("#startTime").find("input").val(defaultStartTime);
            }
            if (null != ready.end) {
                end_time.find("input").val(ready.end);
            } else {
                end_time.find("input").val(current_day);
            }

            $("#selectAllChildCourts").change(function () {
                Class.all_child_option_change();
            });
            $(".container-fluid").show();

            //点击查询按钮
            $("#conditionBtn").click(function () {
                $("#loading").show();
                Class.get_sum_case();
            });
            if (ready.judge_uid) {
                $.get(r.base_path + "user/getUserByUid", {uid: ready.judge_uid}, function (result) {
                    if (result) {
                        if (!result.data) {
                            layui.use('layer', function () {
                                layui.layer.msg("未能找到指定法官，将为您加载所有的庭审数据");
                            });

                        }
                        ready._user_court_uid = result.data.courtUid;
                        Class.init(ready._user_court_uid);
                    }
                }, 'json')
            } else if (ready.secretary) {
                $.get(r.base_path + "user/getUserByUid", {uid: ready.secretary}, function (result) {
                    if (result) {
                        if (!result.data) {
                            layui.use('layer', function () {
                                layui.layer.msg("未能找到指定书记员，将为您加载所有的庭审数据");
                            });
                        }
                        ready._user_court_uid = result.data.courtUid;
                        Class.init(ready._user_court_uid);
                    }
                }, 'json')
            } else {
                if (ready.current_court_uid) {
                    Class.init(ready.current_court_uid);
                } else {
                    Class.init();
                }
            }
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
            ready.city = utils.get_url_param("city") || r.court_uid;
            ready.judge_uid = utils.get_url_param("judge");
            ready.secretary = utils.get_url_param("secretary");
            ready.start = utils.get_url_param("start");
            ready.end = utils.get_url_param("end");
            ready.current_court_uid = utils.get_url_param("currentCourtUid");
            ready.case_type = utils.get_url_param("caseType");

        }
    };
    //TODO 内部调用的方法写这里
    var Class = {
        init: function (_court_uid) {
            ready.city = _court_uid ? _court_uid : r.court_uid;
            !_court_uid ? ready._user_court_uid = null : "";
            Class.select_all_child_courts();
            Class.select_all_court_room();
            Class.select_all_case_type();
            Class.select_judge();
            Class.select_secretary();
            Class.get_sum_case();
        },
        get_sum_case: function () {
            if (r.flag_search) {
                Class.all_count_case_review();
            } else {
                Class.count_case_review();
            }
        },
        all_child_option_change: function () {
            var _current_court_uid = $("#selectAllChildCourts").val();
            Class.select_all_court_room(_current_court_uid);
            Class.select_all_case_type(_current_court_uid);
            Class.select_judge(_current_court_uid);
            Class.select_secretary(_current_court_uid);
            // Class.get_sum_case();
        },
        select_all_child_courts: function () {
            // console.info(ready.city, r.court_uid);
            var select_child_courts = $("#selectAllChildCourts");
            select_child_courts.empty();
            $.each(r.court_list, function (i, n) {
                select_child_courts.append("<option value='" + n.uid + "'>" + n.name + "</option>");
            });
            select_child_courts.val(ready.city);
        },
        select_all_court_room: function (current_court_id) {
            if (current_court_id) {
                ready.city = current_court_id;
            }
            // console.log(ready.city);
            var court_room = $("#allCourtRooms").empty();
            court_room.append("<option value=''>" + "所有法庭" + "</option>");
            $.each(courtroomList, function (i, valueOfElement) {
                if (valueOfElement.courtUid == ready.city) {
                    court_room.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                }
            });
        },
        select_all_case_type: function (current_court_id) {
            if (current_court_id) {
                ready.city = current_court_id;
            }
            var case_type = $("#caseTypeName").empty();
            if (null != ready.case_type && '' != ready.case_type && 'null' != ready.case_type) {
                $.each(caseTypeList, function (i, valueOfElement) {
                    if (ready.case_type == valueOfElement.uid) {
                        case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    }
                });
            } else {
                case_type.append("<option value=''>" + "所有类型" + "</option>");
                $.each(caseTypeList, function (i, valueOfElement) {
                    case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                });
            }
        },
        select_judge: function (current_court_id) {
            $.ajaxSetup({
                async: false
            });
            if (current_court_id) {
                ready.city = current_court_id;
            }
            !ready._user_court_uid ? ready.judge_uid = null : "";

            $.get(r.base_path + "caseCount/getJudgeAndSecretary", {
                courtUid: ready.city,
            }, function (result) {
                if (result.data) {
                    ready.judge_secretary_list = result.data;
                    // console.log(result);
                    var select_judge = $("#allJudges").empty();
                    if (ready.judge_uid) {
                        $.each(result.data.judgeList, function (i, valueOfElement) {
                            if (valueOfElement.uid == ready.judge_uid) {
                                select_judge.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                                $(".active").find("span").append("&nbsp;" + valueOfElement.name + "审理过的案件");//今日开庭和排期
                            }
                        });

                    } else {
                        select_judge.append("<option value=''>" + "所有法官" + "</option>");
                        $.each(result.data.judgeList, function (i, valueOfElement) {
                            select_judge.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                        });
                    }
                }
            }, 'json');
        },
        select_secretary: function (current_court_id) {
            if (current_court_id) {
                ready.city = current_court_id;
            }
            !ready._user_court_uid ? ready.secretary = null : "";
            if (ready.judge_secretary_list) {
                //console.log(result);
                var select_secretary = $("#allSecretary").empty();
                if (ready.secretary) {
                    $.each(ready.judge_secretary_list.secretaryList, function (i, valueOfElement) {
                        if (valueOfElement.uid == ready.secretary) {
                            select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                            select_secretary.find("input").val(valueOfElement.uid);
                            $(".active").find("span").append("&nbsp;" + valueOfElement.name + "审理过的案件");//今日开庭和排期
                        }
                    });

                } else {
                    select_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                    $.each(ready.judge_secretary_list.secretaryList, function (i, valueOfElement) {
                        select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                }
            }
        },
        count_case_review: function (page) {
            if (!page) {
                page = 1;
            }
            //条件
            var caseno = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var judge = $("#allJudges").val();
            var secretary = $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            var row_no_padding = $(".row-no-padding").empty();
            if (secretary == 'null') {
                secretary = null;
            }
            if (judge == 'null') {
                judge = null;
            }
            $.ajax({
                url: r.base_path + "review/getReviewOrPlan",
                type: "post",
                dataType: "json",
                data: {
                    archives: caseno,
                    caseName: caseName,
                    type: caseTypeName,
                    courtRoom: courtRoom,
                    judgeId: judge,
                    secretaryId: secretary,
                    startDate: startTime,
                    endDate: endTime,
                    courtUid: ready.city,
                    status: 0,
                    review: 1,
                    page: page,
                    pageSize: ready.page_size
                },
                success: function (result) {
                    // console.log(result.data);
                    var s = result.data.count;
                    if (s <= 0) {
                        Class.page(1, 1, null);
                        $('#loading').hide();
                    } else {
                        Class.page(page, Math.ceil(s / 12), Class.count_case_review);
                    }
                    if (result.code == 0) {
                        if (result.data.count <= 0) {
                            row_no_padding.append('<span>暂无开庭信息</span>');
                        } else {
                            var picNum = 6;
                            $.each(result.data.data, function (i, valueOfElement) {
                                var caseName = valueOfElement.name;
                                var startDate = valueOfElement.startDate;
                                var uid = valueOfElement.planUid;
                                var status = valueOfElement.status;
                                var courtroom_name = valueOfElement.courtroomName;
                                var cid = valueOfElement.courtroomUid;
                                var statusName = "";
                                if (0 == status) {
                                    statusName = "排期";
                                } else if (1 == status) {
                                    statusName = "开庭";
                                } else if (2 == status) {
                                    statusName = "休庭";
                                } else if (3 == status) {
                                    statusName = "闭庭";
                                } else {
                                    statusName = "归档";
                                }
                                var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                                var href = r.base_path + "system_monitor/panel_play?planUid=" + uid + "&vlcCourtUid=" + ready.city + "&vc=" + valueOfElement.courtUid + '&court=' + r.court_uid;
                                var html = '<div class="col-md-2 col-xs-6">' +
                                    '<div class="more-reviewing-item">' +
                                    '<a href="' + href + '">' +
                                    '<div class="more-reviewing-item-img-panel">' +
                                    '<p class="more-reviewing-item-img-panel-top">' + (courtroom_name ? courtroom_name : '未知') + '</p>' +
                                    '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                                    '<p class="more-reviewing-item-img-panel-bottom">' + statusName + '</p>' +
                                    '</div>' +
                                    '<p id="startDate">' + startDate + '</p>' +
                                    '<p id="caseNoDis">' + (valueOfElement.archives ? valueOfElement.archives : '未知') + '</p>' +
                                    '<p class="more-reviewing-item-depiction">' +
                                    '<i class="fa fa-thumb-tack ' + (valueOfElement.priorDate ? 'status-top' : '') + '" title="收藏" id="' + uid + '..' + valueOfElement.courtId + '"></i>' +
                                    '<span>' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                    '</p>' +
                                    '</a>' +
                                    '</div>' +
                                    '</div>';

                                // temp.find("a").attr("href", r.base_path + "views/system_monitor/play.html?planId=" + uid + "&city=" + city + "&vc=" + valueOfElement.courtId);
                                // temp.find("#_court_").html(courtroom_name);
                                // temp.find("img").attr("src", r.base_path + "images/more_court_test" + randomNum + ".jpg");
                                // temp.find("#_court_status").html(statusName);
                                // temp.find("#startDate").html(startDate);
                                // temp.find("#caseNoDis").html(valueOfElement.caseno);
                                // temp.find(".fa-thumb-tack").attr("id", uid + '..' + valueOfElement.courtId);
                                // if (valueOfElement.priorDate) {
                                //     temp.find(".fa-thumb-tack").addClass("status-top");
                                // }
                                // temp.find("#_court_courtroom").html(caseName ? caseName : "&nbsp;");
                                row_no_padding.append(html);
                            });
                            $(".fa-thumb-tack").click(function (e) {
                                e.preventDefault();
                                var cid = this.id.split('..');
                                var uid = cid[0];
                                var class_name = '' + this.className;
                                layui.use(['layer', 'form'], function () {
                                    var layer = layui.layer;
                                    if (class_name.indexOf("status-top") < 0) {
                                        layer.confirm('确定收藏吗？', {
                                            btn: ['是的', '取消'] //按钮
                                        }, function () {
                                            $.post(r.base_path + "index/updateReview", {
                                                uid: uid,
                                                courtId: cid[1],
                                                favorite: true
                                            }, function (result) {
                                                layer.msg(result.msg);
                                                Class.all_count_case_review(1);
                                            }, 'json')
                                        }, function () {

                                        });
                                    } else {
                                        layer.confirm('确定取消收藏吗？', {
                                            btn: ['是的', '取消'] //按钮
                                        }, function () {
                                            $.post(r.base_path + "index/updateReview", {
                                                uid: uid,
                                                courtId: cid[1],
                                                favorite: false
                                            }, function (result) {
                                                layer.msg(result.msg);
                                                Class.all_count_case_review(1);
                                            }, 'json')
                                        }, function () {

                                        });
                                    }
                                });
                            });
                        }
                    }
                    $("#loading").hide();
                    row_no_padding.show();
                },
                error: function () {
                    $("#loading").hide();
                    layui.use('layer', function () {
                        layui.layer.msg("载入数据发生了问题");
                    });
                }
            });
        },
        all_count_case_review: function (page) {
            if (!page) {
                page = 1;
            }
            //条件
            var caseno = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var judge = $("#allJudges").val();
            var secretary = $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            var row_no_padding = $(".row-no-padding").empty();
            if (secretary == 'null') {
                secretary = null;
            }
            if (judge == 'null') {
                judge = null;
            }
            $.ajax({
                url: r.base_path + "review/getReviewOrPlan",
                type: "post",
                dataType: "json",
                data: {
                    archives: caseno,
                    caseName: caseName,
                    type: caseTypeName,
                    courtRoom: courtRoom,
                    judgeId: judge,
                    secretaryId: secretary,
                    startDate: startTime,
                    endDate: endTime,
                    courtUid: ready.city,
                    status: 1,
                    review: 1,
                    page: page,
                    pageSize: ready.page_size
                },
                success: function (result) {
                    // console.log(result.data);
                    var s = result.data.count;
                    if (s <= 0) {
                        Class.page(1, 1, null);
                        $('#loading').hide();
                    } else {
                        Class.page(page, Math.ceil(s / 12), Class.all_count_case_review);
                    }
                    if (result.code == 0) {
                        if (result.data.count <= 0) {
                            row_no_padding.append('<span>暂无开庭信息</span>');
                        } else {
                            var picNum = 6;
                            $.each(result.data.data, function (i, valueOfElement) {
                                var caseName = valueOfElement.name;
                                var startDate = valueOfElement.startDate;
                                var uid = valueOfElement.planUid;
                                var status = valueOfElement.status;
                                var courtroom_name = valueOfElement.courtroomName;
                                var cid = valueOfElement.courtroomUid;
                                var statusName = "";
                                if (0 == status) {
                                    statusName = "排期";
                                } else if (1 == status) {
                                    statusName = "开庭";
                                } else if (2 == status) {
                                    statusName = "休庭";
                                } else if (3 == status) {
                                    statusName = "闭庭";
                                } else {
                                    statusName = "归档";
                                }
                                var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                                var href = r.base_path + "system_monitor/panel_play?planUid=" + uid + "&vlcCourtUid=" + ready.city + "&vc=" + valueOfElement.courtUid + '&court=' + r.court_uid;
                                var html = '<div class="col-md-2 col-xs-6">' +
                                    '<div class="more-reviewing-item">' +
                                    '<a href="' + href + '">' +
                                    '<div class="more-reviewing-item-img-panel">' +
                                    '<p class="more-reviewing-item-img-panel-top">' + (courtroom_name ? courtroom_name : '未知') + '</p>' +
                                    '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                                    '<p class="more-reviewing-item-img-panel-bottom">' + statusName + '</p>' +
                                    '</div>' +
                                    '<p id="startDate">' + startDate + '</p>' +
                                    '<p id="caseNoDis">' + (valueOfElement.archives ? valueOfElement.archives : '未知') + '</p>' +
                                    '<p class="more-reviewing-item-depiction">' +
                                    '<i class="fa fa-thumb-tack ' + (valueOfElement.priorDate ? 'status-top' : '') + '" title="收藏" id="' + uid + '..' + valueOfElement.courtId + '"></i>' +
                                    '<span>' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                    '</p>' +
                                    '</a>' +
                                    '</div>' +
                                    '</div>';

                                // temp.find("a").attr("href", r.base_path + "views/system_monitor/play.html?planId=" + uid + "&city=" + city + "&vc=" + valueOfElement.courtId);
                                // temp.find("#_court_").html(courtroom_name);
                                // temp.find("img").attr("src", r.base_path + "images/more_court_test" + randomNum + ".jpg");
                                // temp.find("#_court_status").html(statusName);
                                // temp.find("#startDate").html(startDate);
                                // temp.find("#caseNoDis").html(valueOfElement.caseno);
                                // temp.find(".fa-thumb-tack").attr("id", uid + '..' + valueOfElement.courtId);
                                // if (valueOfElement.priorDate) {
                                //     temp.find(".fa-thumb-tack").addClass("status-top");
                                // }
                                // temp.find("#_court_courtroom").html(caseName ? caseName : "&nbsp;");
                                row_no_padding.append(html);
                            });
                            $(".fa-thumb-tack").click(function (e) {
                                e.preventDefault();
                                var cid = this.id.split('..');
                                var uid = cid[0];
                                var class_name = '' + this.className;
                                layui.use(['layer', 'form'], function () {
                                    var layer = layui.layer;
                                    if (class_name.indexOf("status-top") < 0) {
                                        layer.confirm('确定收藏吗？', {
                                            btn: ['是的', '取消'] //按钮
                                        }, function () {
                                            $.post(r.base_path + "index/updateReview", {
                                                uid: uid,
                                                courtId: cid[1],
                                                favorite: true
                                            }, function (result) {
                                                layer.msg(result.msg);
                                                Class.all_count_case_review(1);
                                            }, 'json')
                                        }, function () {

                                        });
                                    } else {
                                        layer.confirm('确定取消收藏吗？', {
                                            btn: ['是的', '取消'] //按钮
                                        }, function () {
                                            $.post(r.base_path + "index/updateReview", {
                                                uid: uid,
                                                courtId: cid[1],
                                                favorite: false
                                            }, function (result) {
                                                layer.msg(result.msg);
                                                Class.all_count_case_review(1);
                                            }, 'json')
                                        }, function () {

                                        });
                                    }
                                });
                            });
                        }
                    }
                    $("#loading").hide();
                    row_no_padding.show();
                },
                error: function () {
                    $("#loading").hide();
                    layui.use('layer', function () {
                        layui.layer.msg("载入数据发生了问题");
                    });
                }
            });
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {
        get_sum_case: Class.get_sum_case
    };
}(window);
